<?php

declare(strict_types=1);

namespace ImiApp\WebSocketServer\Controller;

 
use Imi\ConnectionContext;
use Imi\Controller\WebSocketController;
use Imi\Server\WebSocket\Route\Annotation\WSRoute;
// use Imi\Server\WebSocket\Route\Annotation\WSConfig;
use Imi\Server\WebSocket\Route\Annotation\WSAction;
use Imi\Server\WebSocket\Route\Annotation\WSController;
 
/**
 * 数据收发测试
 * @WSController
 */
class Test extends WebSocketController
{
    /**
     * 登录
     *
     * @WSAction
     * @WSRoute({"action"="login"})
     * @param mixed $data
     * @return void
     */
    public function login($data)
    {
        var_dump('ImiApp\WebSocketServer\Controller Test ws',$data);
        // ConnectionContext::set('username', $data->username);
        $user_id = ConnectionContext::get('user_id');

        // $this->server->joinGroup('g1', $this->frame->getClientId());
        return ['success'=>true,'user_id'=>$user_id];
    }


    /**
     * 发送消息
     *
     * @WSAction
     * @WSRoute({"action"="send"})
     *
     * @param mixed $data
     */
    public function send($data): array
    {
        $address = $this->frame->getClientAddress();
        $message = '[' . $address->getAddress() . ':' . $address->getPort() . ']: ' ;

        var_dump('ImiApp\ApiServer\Ws\Controller\Test send',$data);
        return [
            'success' => true,
            'data'    => $message,
            'mode'    => \Imi\App::getApp()->getType(),
        ];
    }
}